<?php
namespace BP\Frm;
require_once("BP/Difference/Handler/CommonFileUtils.php");
require_once("BP/Difference/Handler/CommonUtils.php");
require_once("BP/Difference/Handler/WebContralBase.php");
require_once("BP/Tools/DateUtils.php");
require_once("BP/Tools/StringHelper.php");
require_once("javax/servlet/http/HttpServletRequest.php");
require_once("java/net/URLDecoder.php");
class WF_CCBill extends WebContralBase {
	public static function constructor__ () 
	{
		$me = new self();
		return $me;
	}
	public function Start_Init () 
	{
		$ds = $BP->Frm->Dev2Interface->DB_StartBills($WebUser->getNo());
		return $BP->Tools->Json->ToJson($ds);
	}
	public function Draft_Init () 
	{
		$dt = $BP->Frm->Dev2Interface->DB_Draft($this->getFrmID(), $WebUser->getNo());
		return $BP->Tools->Json->DataTableToJson($dt,  FALSE );
	}
	public function MyBill_Init () 
	{
		$ds = $BP->Frm->Dev2Interface->DB_StartBills($WebUser->getNo());
		return $BP->Tools->Json->ToJson($ds);
	}
	public function DoMethod_ExeSQL () 
	{
		$func = MethodFunc::constructor__String($this->getMyPK());
		$doc = $func->getMethodDoc_SQL();
		$en = new GEEntity($func->getFrmID(), $this->getWorkID());
		$doc = $BP->WF->Glo->DealExp($doc, $en, NULL);
		try 
		{
			DBAccess->RunSQLs($doc);
			if ($func->getMsgSuccess()->equals(""))
			{
				$func->setMsgSuccess("执行成功.");
			}
			return $func->getMsgSuccess();
		}
		catch (\RuntimeException $ex)
		{
			if ($func->getMsgErr()->equals(""))
			{
				$func->setMsgErr("执行失败(DoMethod_ExeSQL).");
			}
			return ((("err@" . $func->getMsgErr()) . " @ ") . $ex->getMessage());
		}
	}
	public function DoMethodPara_ExeSQL () 
	{
		$func = MethodFunc::constructor__String($this->getMyPK());
		$doc = $func->getMethodDoc_SQL();
		$en = new GEEntity($func->getFrmID(), $this->getWorkID());
		$doc = $BP->WF->Glo->DealExp($doc, $en, NULL);
		$attrs = new MapAttrs();
		$attrs->Retrieve($MapAttrAttr->FK_MapData, $this->getMyPK());
		foreach ($attrs->ToJavaList() as $item) 		{
			if (($item->getUIContralType() == $UIContralType->TB))
			{
				$doc = $doc->replace(("@" . $item->getKeyOfEn()), $this->GetRequestVal(("TB_" . $item->getKeyOfEn())));
				continue;
			}
			if (($item->getUIContralType() == $UIContralType->DDL))
			{
				$doc = $doc->replace(("@" . $item->getKeyOfEn()), $this->GetRequestVal(("DDL_" . $item->getKeyOfEn())));
				continue;
			}
			if (($item->getUIContralType() == $UIContralType->CheckBok))
			{
				$doc = $doc->replace(("@" . $item->getKeyOfEn()), $this->GetRequestVal(("CB_" . $item->getKeyOfEn())));
				continue;
			}
			if (($item->getUIContralType() == $UIContralType->RadioBtn))
			{
				$doc = $doc->replace(("@" . $item->getKeyOfEn()), $this->GetRequestVal(("RB_" . $item->getKeyOfEn())));
				continue;
			}
		}
		try 
		{
			DBAccess->RunSQLs($doc);
			if ($func->getMsgSuccess()->equals(""))
			{
				$func->setMsgSuccess("执行成功.");
			}
			return $func->getMsgSuccess();
		}
		catch (\RuntimeException $ex)
		{
			if ($func->getMsgErr()->equals(""))
			{
				$func->setMsgErr("执行失败.");
			}
			return ((("err@" . $func->getMsgErr()) . " @ ") . $ex->getMessage());
		}
	}
	public function MyBill_CreateBlankBillID () 
	{
		return $String->valueOf($BP->Frm->Dev2Interface->CreateBlankBillID($this->getFrmID(), $WebUser->getNo(), NULL));
	}
	public function MyDict_CreateBlankDictID () 
	{
		return $String->valueOf($BP->Frm->Dev2Interface->CreateBlankDictID($this->getFrmID(), $WebUser->getNo(), NULL));
	}
	public function MyBill_SaveIt () 
	{
		$rpt = new GEEntity($this->getFrmID(), $this->getWorkID());
		$tempVar = PubClass->CopyFromRequest($rpt);
		$rpt = ( ($tempVar instanceof GEEntity) ? $tempVar : NULL );
		$ht = $this->GetMainTableHT();
		foreach ($ht->keySet() as $item) 		{
			if (($item != NULL))
				$rpt->SetValByKey($item, $ht->get($item));
		}
		$rpt->setOID($this->getWorkID());
		$rpt->SetValByKey("BillState", BillState::$Editing->getValue());
		$rpt->Update();
		$str = $BP->Frm->Dev2Interface->SaveWork($this->getFrmID(), $this->getWorkID());
		return $str;
	}
	public function MyDict_SaveIt () 
	{
		$rpt = new GEEntity($this->getFrmID(), $this->getWorkID());
		$tempVar = PubClass->CopyFromRequest($rpt);
		$rpt = ( ($tempVar instanceof GEEntity) ? $tempVar : NULL );
		$ht = $this->GetMainTableHT();
		foreach ($ht->keySet() as $item) 		{
			if (($item != NULL))
				$rpt->SetValByKey($item, $ht->get($item));
		}
		$rpt->setOID($this->getWorkID());
		$rpt->SetValByKey("BillState", BillState::$Editing->getValue());
		$rpt->Update();
		$str = $BP->Frm->Dev2Interface->SaveWork($this->getFrmID(), $this->getWorkID());
		return $str;
	}
	public function GetFrmEntitys () 
	{
		$rpts = new GEEntitys($this->getFrmID());
		$qo = new QueryObject($rpts);
		$qo->AddWhere("BillState", " != ", 0);
		$qo->DoQuery();
		return $BP->Tools->Json->ToJson($rpts->ToDataTableField());
	}
	protected function GetMainTableHT () 
	{
		$htMain = new Hashtable();
		$enu = $this->getRequest()->getParameterNames();
		while ($enu->hasMoreElements()) 
		{
			$key = $enu->nextElement();
			if (($key . NULL))
			{
				continue;
			}
			if ($key->contains("TB_"))
			{
				if (($htMain->containsKey($key->replace("TB_", "")) ==  FALSE ))
				{
					$htMain->put($key->replace("TB_", ""), $URLDecoder->decode($this->GetRequestVal($key), "UTF-8"));
				}
				continue;
			}
			if ($key->contains("DDL_"))
			{
				$htMain->put($key->replace("DDL_", ""), $URLDecoder->decode($this->GetRequestVal($key), "UTF-8"));
				continue;
			}
			if ($key->contains("CB_"))
			{
				$htMain->put($key->replace("CB_", ""), $URLDecoder->decode($this->GetRequestVal($key), "UTF-8"));
				continue;
			}
			if ($key->contains("RB_"))
			{
				$htMain->put($key->replace("RB_", ""), $URLDecoder->decode($this->GetRequestVal($key), "UTF-8"));
				continue;
			}
		}
		return $htMain;
	}
	public function MyBill_SaveAsDraft () 
	{
		$str = $BP->Frm->Dev2Interface->SaveWork($this->getFrmID(), $this->getWorkID());
		return $str;
	}
	public function MyBill_Delete () 
	{
		return $BP->Frm->Dev2Interface->MyBill_Delete($this->getFrmID(), $this->getWorkID());
	}
	public function MyBill_Deletes () 
	{
		return $BP->Frm->Dev2Interface->MyBill_DeleteDicts($this->getFrmID(), $this->GetRequestVal("WorkIDs"));
	}
	public function MyDict_Delete () 
	{
		return $BP->Frm->Dev2Interface->MyDict_Delete($this->getFrmID(), $this->getWorkID());
	}
	public function MyEntityTree_Delete () 
	{
		return $BP->Frm->Dev2Interface->MyEntityTree_Delete($this->getFrmID(), $this->GetRequestVal("BillNo"));
	}
	public function MyBill_Copy () 
	{
		return $BP->Frm->Dev2Interface->MyBill_Copy($this->getFrmID(), $this->getWorkID());
	}
	public function Search_ToolBar () 
	{
		$ds = new DataSet();
		$dt = new DataTable();
		$md = new MapData($this->getFrmID());
		$ds->Tables->add($md->ToDataTableField("Sys_MapData"));
		$attrs = new MapAttrs($this->getFrmID());
		$dt->Columns->Add("Field", String);
		$dt->Columns->Add("Name", String);
		$dt->Columns->Add("Width", Integer);
		$dt->TableName = "Attrs";
		$ctrls = $md->getRptSearchKeys()->split("[*]", -1);
		$dtNoName = NULL;
		$mapattr = null;
		$dr = NULL;
		foreach ($ctrls as $ctrl) 		{
			if (($DataType->IsNullOrEmpty($ctrl) || !$DataType->IsNullOrEmpty($this->GetRequestVal($ctrl))))
			{
				continue;
			}
			$tempVar = $attrs->GetEntityByKey($MapAttrAttr->KeyOfEn, $ctrl);
			$mapattr = ( ($tempVar instanceof MapAttr) ? $tempVar : NULL );
			$dr = $dt->NewRow();
			$dr->setValue("Field", $mapattr->getKeyOfEn());
			$dr->setValue("Name", $mapattr->getName());
			$dr->setValue("Width", $mapattr->getUIWidth());
			$dt->Rows->add($dr);
			$attr = $mapattr->getHisAttr();
			if (($mapattr == NULL))
			{
				continue;
			}
			if (($attr->getIsEnum() ==  TRUE ))
			{
				$ses = new SysEnums($mapattr->getUIBindKey());
				$dtEnum = $ses->ToDataTableField();
				$dtEnum->TableName = $mapattr->getKeyOfEn();
				$ds->Tables->add($dtEnum);
				continue;
			}
			if (($attr->getIsFK() ==  TRUE ))
			{
				$ensFK = $attr->getHisFKEns();
				$ensFK->RetrieveAll();
				$dtEn = $ensFK->ToDataTableField();
				$dtEn->TableName = $attr->getKey();
				$ds->Tables->add($dtEn);
			}
			if (($attr->getUIDDLShowType() == $DDLShowType->BindSQL))
			{
				$sql = $attr->getUIBindKey();
				$sql = $BP->WF->Glo->DealExp($sql, NULL, NULL);
				$dtSQl = DBAccess->RunSQLReturnTable($sql);
				foreach ($dtSQl->Columns as $col) 				{
					$colName = $col->ColumnName->toLowerCase();
					switch ($colName) {
						case "no":
							$col->ColumnName = "No";
							break;
						case "name":
							$col->ColumnName = "Name";
							break;
						case "parentno":
							$col->ColumnName = "ParentNo";
							break;
						default:
							break;
					}
				}
				$dtSQl->TableName = $attr->getKey();
				if (($ds->Tables->contains($attr->getKey()) ==  FALSE ))
				{
					$ds->Tables->add($dtSQl);
				}
			}
		}
		$ds->Tables->add($dt);
		return $BP->Tools->Json->ToJson($ds);
	}
	public function Search_Init () 
	{
		$ds = new DataSet();
		$mapattrs = new MapAttrs();
		$mapattrs->Retrieve($MapAttrAttr->FK_MapData, $this->getFrmID(), $MapAttrAttr->Idx);
		$row = NULL;
		$dt = new DataTable("Attrs");
		$dt->Columns->Add("KeyOfEn", String);
		$dt->Columns->Add("Name", String);
		$dt->Columns->Add("Width", Integer);
		$dt->Columns->Add("UIContralType", Integer);
		$dt->Columns->Add("LGType", Integer);
		$dt->Columns->Add("AtPara", String);
		foreach ($mapattrs->ToJavaList() as $attr) 		{
			$searchVisable = $attr->getatPara()->GetValStrByKey("SearchVisable");
			if ($searchVisable->equals("0"))
			{
				continue;
			}
			if (($attr->getUIVisible() ==  FALSE ))
			{
				continue;
			}
			$row = $dt->NewRow();
			$row->setValue("KeyOfEn", $attr->getKeyOfEn());
			$row->setValue("Name", $attr->getName());
			$row->setValue("Width", $attr->getUIWidthInt());
			$row->setValue("UIContralType", $attr->getUIContralType());
			$row->setValue("LGType", $attr->getLGType());
			$row->setValue("AtPara", $attr->GetValStringByKey("AtPara"));
			$dt->Rows->add($row);
		}
		$ds->Tables->add($dt);
		$md = new MapData($this->getFrmID());
		$ur = new UserRegedit();
		$ur->setMyPK(((($WebUser->getNo() . "_") . $this->getFrmID()) . "_SearchAttrs"));
		$ur->RetrieveFromDBSources();
		$rpts = new GEEntitys($this->getFrmID());
		$attrs = $rpts->getNewEntity()->getEnMap()->getAttrs();
		$qo = new QueryObject($rpts);
		$keyWord = $ur->getSearchKey();
		if ((($md->GetParaBoolen("IsSearchKey") && ($DataType->IsNullOrEmpty($keyWord) ==  FALSE )) && ($keyWord->length() >= 1)))
		{
			$attrPK = new Attr();
			foreach ($attrs as $attr) 			{
				if ($attr->getIsPK())
				{
					$attrPK = $attr;
					break;
				}
			}
			$i = 0;
			$enumKey = ",";
			foreach ($attrs as $attr) 			{
				switch ($attr->getMyFieldType()) {
					case $Enum:
						$enumKey = (("," . $attr->getKey()) . "Text,");
						break;
					case $FK:
						continue;
					default:
						break;
				}
				if (($attr->getMyDataType() != $DataType->AppString))
				{
					continue;
				}
				if (($attr->getMyFieldType() == $FieldType->RefText))
				{
					if (($enumKey->contains((("," . $attr->getKey()) . ",")) ==  TRUE ))
					{
						continue;
					}
				}
				if ($attr->getKey()->equals("FK_Dept"))
				{
					continue;
				}
				++$i;
				if (($i == 1))
				{
					$qo->addLeftBracket();
					if (($SystemConfig->getAppCenterDBVarStr()->equals("@") || $SystemConfig->getAppCenterDBVarStr()->equals("?")))
					{
						$qo->AddWhere($attr->getKey(), " LIKE ", ( (($SystemConfig->getAppCenterDBType() == $DBType->MySQL)) ? (((" CONCAT('%'," . $SystemConfig->getAppCenterDBVarStr()) . "SKey,'%')")) : (((" '%'+" . $SystemConfig->getAppCenterDBVarStr()) . "SKey+'%'")) ));
					}
					else
					{
						$qo->AddWhere($attr->getKey(), " LIKE ", ((" '%'||" . $SystemConfig->getAppCenterDBVarStr()) . "SKey||'%'"));
					}
					continue;
				}
				$qo->addOr();
				if (($SystemConfig->getAppCenterDBVarStr()->equals("@") || $SystemConfig->getAppCenterDBVarStr()->equals("?")))
				{
					$qo->AddWhere($attr->getKey(), " LIKE ", ( (($SystemConfig->getAppCenterDBType() == $DBType->MySQL)) ? ((("CONCAT('%'," . $SystemConfig->getAppCenterDBVarStr()) . "SKey,'%')")) : ((("'%'+" . $SystemConfig->getAppCenterDBVarStr()) . "SKey+'%'")) ));
				}
				else
				{
					$qo->AddWhere($attr->getKey(), " LIKE ", (("'%'||" . $SystemConfig->getAppCenterDBVarStr()) . "SKey||'%'"));
				}
			}
			$qo->getMyParas()->Add("SKey", $keyWord);
			$qo->addRightBracket();
		}
		else
		{
			$qo->AddHD();
		}
		if ((($md->GetParaInt("DTSearchWay") != $DTSearchWay->None->getValue()) && ($DataType->IsNullOrEmpty($ur->getDTFrom()) ==  FALSE )))
		{
			$dtFrom = $ur->getDTFrom();
			$dtTo = $ur->getDTTo();
			if (($md->GetParaInt("DTSearchWay") == $DTSearchWay->ByDate->getValue()))
			{
				$qo->addAnd();
				$qo->addLeftBracket();
				$dtTo += " 23:59:59";
				$qo->setSQL(((($md->GetParaString("DTSearchKey") . " >= '") . $dtFrom) . "'"));
				$qo->addAnd();
				$qo->setSQL(((($md->GetParaString("DTSearchKey") . " <= '") . $dtTo) . "'"));
				$qo->addRightBracket();
			}
			if (($md->GetParaInt("DTSearchWay") == $DTSearchWay->ByDateTime->getValue()))
			{
				if (($dtFrom->trim()->length() == 10))
				{
					$dtFrom += " 00:00:00";
				}
				if (($dtFrom->trim()->length() == 16))
				{
					$dtFrom += ":00";
				}
				$dtFrom = ($DateUtils->addDay($DateUtils->parse($dtFrom, "yyyy-MM-dd"), -1) . " 24:00");
				if ((($dtTo->trim()->length() < 11) || ($dtTo->trim()->indexOf(' ') == -1)))
				{
					$dtTo += " 24:00";
				}
				$qo->addAnd();
				$qo->addLeftBracket();
				$qo->setSQL(((($md->GetParaString("DTSearchKey") . " >= '") . $dtFrom) . "'"));
				$qo->addAnd();
				$qo->setSQL(((($md->GetParaString("DTSearchKey") . " <= '") . $dtTo) . "'"));
				$qo->addRightBracket();
			}
		}
		$ap = new AtPara($ur->getVals());
		foreach ($ap->getHisHT()->keySet() as $str) 		{
			$val = $ap->GetValStrByKey($str);
			if ($val->equals("all"))
			{
				continue;
			}
			$qo->addAnd();
			$qo->addLeftBracket();
			if (($SystemConfig->getAppCenterDBType() == $DBType->PostgreSQL))
			{
				$typeVal = Glo->GenerRealType($attrs, $str, $ap->GetValStrByKey($str));
				$qo->AddWhere($str, $typeVal);
			}
			else
			{
				$qo->AddWhere($str, $ap->GetValStrByKey($str));
			}
			$qo->addRightBracket();
		}
		$qo->addAnd();
		$qo->AddWhere("BillState", "!=", 0);
		$ur->SetPara("RecCount", $qo->GetCount());
		$ur->Save();
		if ((($DataType->IsNullOrEmpty($ur->getOrderBy()) ==  FALSE ) && ($DataType->IsNullOrEmpty($ur->getOrderWay()) ==  FALSE )))
		{
			$qo->DoQuery("OID", $this->getPageSize(), $this->getPageIdx(), $ur->getOrderBy(), $ur->getOrderWay());
		}
		else
		{
			$qo->DoQuery("OID", $this->getPageSize(), $this->getPageIdx());
		}
		$mydt = $rpts->ToDataTableField();
		$mydt->TableName = "DT";
		$ds->Tables->add($mydt);
		return $BP->Tools->Json->ToJson($ds);
	}
	public function GenerBill_Init () 
	{
		$bills = GenerBills::constructor__();
		$bills->Retrieve(GenerBillAttr::$Starter, $WebUser->getNo());
		return $bills->ToJson();
	}
	public function SearchData_Init () 
	{
		$ds = new DataSet();
		$sql = "";
		$tSpan = $this->GetRequestVal("TSpan");
		if ($tSpan->equals(""))
		{
			$tSpan = NULL;
		}
		$ses = new SysEnums("TSpan");
		$dtTSpan = $ses->ToDataTableField();
		$dtTSpan->TableName = "TSpan";
		$ds->Tables->add($dtTSpan);
		$gb = GenerBill::constructor__();
		$gb->CheckPhysicsTable();
		$sql = (((("SELECT TSpan as No, COUNT(WorkID) as Num FROM Frm_GenerBill WHERE FrmID='" . $this->getFrmID()) . "'  AND Starter='") . $WebUser->getNo()) . "' AND BillState >= 1 GROUP BY TSpan");
		$dtTSpanNum =DBAccess->RunSQLReturnTable($sql);
		foreach ($dtTSpan->Rows as $drEnum) 		{
			$no = $drEnum->get("IntKey");
			foreach ($dtTSpanNum->Rows as $dr) 			{
				if ($dr->get("No")->equals($no))
				{
					$drEnum->setValue("Lab", ((($drEnum->get("Lab") . "(") . $dr->get("Num")) . ")"));
					break;
				}
			}
		}
		$sql = " SELECT  A.BillState as No, B.Lab as Name, COUNT(WorkID) as Num FROM Frm_GenerBill A, Sys_Enum B ";
		$sql += ((" WHERE A.BillState=B.IntKey AND B.EnumKey='BillState' AND  A.Starter='" . $WebUser->getNo()) . "' AND BillState >=1");
		if (($tSpan->equals("-1") ==  FALSE ))
		{
			$sql += ("  AND A.TSpan=" . $tSpan);
		}
		$sql += "  GROUP BY A.BillState, B.Lab  ";
		$dtFlows =DBAccess->RunSQLReturnTable($sql);
		if ((($SystemConfig->getAppCenterDBType() == $DBType->Oracle) || ($SystemConfig->getAppCenterDBType() == $DBType->PostgreSQL)))
		{
			$dtFlows->Columns->get(0)->ColumnName = "No";
			$dtFlows->Columns->get(1)->ColumnName = "Name";
			$dtFlows->Columns->get(2)->ColumnName = "Num";
		}
		$dtFlows->TableName = "Flows";
		$ds->Tables->add($dtFlows);
		$sqlWhere = "";
		$sqlWhere = (("(1 = 1)AND Starter = '" . $WebUser->getNo()) . "' AND BillState >= 1");
		if (($tSpan->equals("-1") ==  FALSE ))
		{
			$sqlWhere += (("AND (TSpan = '" . $tSpan) . "') ");
		}
		if (($this->getFK_Flow() != NULL))
		{
			$sqlWhere += (("AND (FrmID = '" . $this->getFrmID()) . "')  ");
		}
		else
		$sqlWhere += "ORDER BY RDT DESC";
		$fields = " WorkID,FrmID,FrmName,Title,BillState, Starter, StarterName,Sender,RDT ";
		if (($SystemConfig->getAppCenterDBType() == $DBType->Oracle))
		{
			$sql = (((("SELECT " . $fields) . " FROM (SELECT * FROM Frm_GenerBill WHERE ") . $sqlWhere) . ") WHERE rownum <= 50");
		}
		else
			if (($SystemConfig->getAppCenterDBType() == $DBType->MSSQL))
			{
				$sql = ((("SELECT  TOP 50 " . $fields) . " FROM Frm_GenerBill WHERE ") . $sqlWhere);
			}
			else
				if ((($SystemConfig->getAppCenterDBType() == $DBType->MySQL) || ($SystemConfig->getAppCenterDBType() == $DBType->PostgreSQL)))
				{
					$sql = (((("SELECT  " . $fields) . " FROM Frm_GenerBill WHERE ") . $sqlWhere) . " LIMIT 50");
				}
		$mydt =DBAccess->RunSQLReturnTable($sql);
		if ((($SystemConfig->getAppCenterDBType() == $DBType->Oracle) || ($SystemConfig->getAppCenterDBType() == $DBType->PostgreSQL)))
		{
			$mydt->Columns->get(0)->ColumnName = "WorkID";
			$mydt->Columns->get(1)->ColumnName = "FrmID";
			$mydt->Columns->get(2)->ColumnName = "FrmName";
			$mydt->Columns->get(3)->ColumnName = "Title";
			$mydt->Columns->get(4)->ColumnName = "BillState";
			$mydt->Columns->get(5)->ColumnName = "Starter";
			$mydt->Columns->get(6)->ColumnName = "StarterName";
			$mydt->Columns->get(7)->ColumnName = "Sender";
			$mydt->Columns->get(8)->ColumnName = "RDT";
		}
		$mydt->TableName = "Frm_Bill";
		if (($mydt != NULL))
		{
			$mydt->Columns->Add("TDTime");
		}
		$ds->Tables->add($mydt);
		return $BP->Tools->Json->ToJson($ds);
	}
	public function Search_Exp () 
	{
		$frmBill = FrmBill::constructor__String($this->getFrmID());
		$rpts = new GEEntitys($this->getFrmID());
		$mapAttrs = new MapAttrs();
		$attrs = new Attrs();
		$mapAttrs->Retrieve($MapAttrAttr->FK_MapData, $this->getEnsName(), $MapAttrAttr->Idx);
		foreach ($mapAttrs->ToJavaList() as $attr) 			$attrs->Add($attr->getHisAttr());
		$name = "数据导出";
		$filename = ((($frmBill->getName() . "_") .DataType->getCurrentDataTimeCNOfLong()) . ".xls");
		$filePath = $this->ExportDGToExcel($this->Search_Data(), $rpts->getNewEntity(), $name, $attrs);
		return $filePath;
	}
	public function Search_Data () 
	{
		$ds = new DataSet();
		$md = new MapData($this->getFrmID());
		$ur = new UserRegedit();
		$ur->setMyPK(((($WebUser->getNo() . "_") . $this->getFrmID()) . "_SearchAttrs"));
		$ur->RetrieveFromDBSources();
		$rpts = new GEEntitys($this->getFrmID());
		$attrs = $rpts->getNewEntity()->getEnMap()->getAttrs();
		$qo = new QueryObject($rpts);
		$keyWord = $ur->getSearchKey();
		if ((($md->GetParaBoolen("IsSearchKey") && ($DataType->IsNullOrEmpty($keyWord) ==  FALSE )) && ($keyWord->length() >= 1)))
		{
			$attrPK = new Attr();
			foreach ($attrs as $attr) 			{
				if ($attr->getIsPK())
				{
					$attrPK = $attr;
					break;
				}
			}
			$i = 0;
			$enumKey = ",";
			foreach ($attrs as $attr) 			{
				switch ($attr->getMyFieldType()) {
					case $Enum:
						$enumKey = (("," . $attr->getKey()) . "Text,");
						break;
					case $FK:
						continue;
					default:
						break;
				}
				if (($attr->getMyDataType() != $DataType->AppString))
				{
					continue;
				}
				if (($attr->getMyFieldType() == $FieldType->RefText))
				{
					if (($enumKey->contains((("," . $attr->getKey()) . ",")) ==  TRUE ))
					{
						continue;
					}
				}
				if ($attr->getKey()->equals("FK_Dept"))
				{
					continue;
				}
				++$i;
				if (($i == 1))
				{
					$qo->addLeftBracket();
					if (($SystemConfig->getAppCenterDBVarStr()->equals("@") || $SystemConfig->getAppCenterDBVarStr()->equals("?")))
					{
						$qo->AddWhere($attr->getKey(), " LIKE ", ( (($SystemConfig->getAppCenterDBType() == $DBType->MySQL)) ? (((" CONCAT('%'," . $SystemConfig->getAppCenterDBVarStr()) . "SKey,'%')")) : (((" '%'+" . $SystemConfig->getAppCenterDBVarStr()) . "SKey+'%'")) ));
					}
					else
					{
						$qo->AddWhere($attr->getKey(), " LIKE ", ((" '%'||" . $SystemConfig->getAppCenterDBVarStr()) . "SKey||'%'"));
					}
					continue;
				}
				$qo->addOr();
				if (($SystemConfig->getAppCenterDBVarStr()->equals("@") || $SystemConfig->getAppCenterDBVarStr()->equals("?")))
				{
					$qo->AddWhere($attr->getKey(), " LIKE ", ( (($SystemConfig->getAppCenterDBType() == $DBType->MySQL)) ? ((("CONCAT('%'," . $SystemConfig->getAppCenterDBVarStr()) . "SKey,'%')")) : ((("'%'+" . $SystemConfig->getAppCenterDBVarStr()) . "SKey+'%'")) ));
				}
				else
				{
					$qo->AddWhere($attr->getKey(), " LIKE ", (("'%'||" . $SystemConfig->getAppCenterDBVarStr()) . "SKey||'%'"));
				}
			}
			$qo->getMyParas()->Add("SKey", $keyWord);
			$qo->addRightBracket();
		}
		else
		{
			$qo->AddHD();
		}
		if ((($md->GetParaInt("DTSearchWay") != $DTSearchWay->None->getValue()) && ($DataType->IsNullOrEmpty($ur->getDTFrom()) ==  FALSE )))
		{
			$dtFrom = $ur->getDTFrom();
			$dtTo = $ur->getDTTo();
			if (($md->GetParaInt("DTSearchWay") == $DTSearchWay->ByDate->getValue()))
			{
				$qo->addAnd();
				$qo->addLeftBracket();
				$dtTo += " 23:59:59";
				$qo->setSQL(((($md->GetParaString("DTSearchKey") . " >= '") . $dtFrom) . "'"));
				$qo->addAnd();
				$qo->setSQL(((($md->GetParaString("DTSearchKey") . " <= '") . $dtTo) . "'"));
				$qo->addRightBracket();
			}
			if (($md->GetParaInt("DTSearchWay") == $DTSearchWay->ByDateTime->getValue()))
			{
				if (($dtFrom->trim()->length() == 10))
				{
					$dtFrom += " 00:00:00";
				}
				if (($dtFrom->trim()->length() == 16))
				{
					$dtFrom += ":00";
				}
				$dtFrom = ($DateUtils->addDay($DateUtils->parse($dtFrom, "yyyy-MM-dd"), -1) . " 24:00");
				if ((($dtTo->trim()->length() < 11) || ($dtTo->trim()->indexOf(' ') == -1)))
				{
					$dtTo += " 24:00";
				}
				$qo->addAnd();
				$qo->addLeftBracket();
				$qo->setSQL(((($md->GetParaString("DTSearchKey") . " >= '") . $dtFrom) . "'"));
				$qo->addAnd();
				$qo->setSQL(((($md->GetParaString("DTSearchKey") . " <= '") . $dtTo) . "'"));
				$qo->addRightBracket();
			}
		}
		$ap = new AtPara($ur->getVals());
		foreach ($ap->getHisHT()->keySet() as $str) 		{
			$val = $ap->GetValStrByKey($str);
			if ($val->equals("all"))
			{
				continue;
			}
			$qo->addAnd();
			$qo->addLeftBracket();
			if (($SystemConfig->getAppCenterDBType() == $DBType->PostgreSQL))
			{
				$typeVal = Glo->GenerRealType($attrs, $str, $ap->GetValStrByKey($str));
				$qo->AddWhere($str, $typeVal);
			}
			else
			{
				$qo->AddWhere($str, $ap->GetValStrByKey($str));
			}
			$qo->addRightBracket();
		}
		$qo->addOrderBy("OID");
		return $qo->DoQueryToTable();
	}
	public function ImpData_Done () 
	{
		$request = $this->getRequest();
		if (($CommonFileUtils->getFilesSize($request, "File_Upload") == 0))
		{
			return "err@请�?�择要导入的数据信息�?";
		}
		$fileName = $CommonFileUtils->getOriginalFilename($request, "File_Upload");
		$prefix = $fileName->substring(($fileName->lastIndexOf(".") + 1));
		if ((!$prefix->equals("xls") && !$prefix->equals("xlsx")))
		{
			return "err@上传的文件必须是Excel文件.";
		}
		$errInfo = "";
		$ext = ".xls";
		if ($fileName->contains(".xlsx"))
		{
			$ext = ".xlsx";
		}
		$fileNewName = ($DateUtils->format(new Date(), "yyyyMMddHHmmssff") . $ext);
		$filePath = ((SystemConfig::getPathOfTemp() . "/") . $fileNewName);
		$file = new File($filePath);
		$CommonFileUtils->upload($request, "File_Upload", $file);
		$dt =DBLoad->GetTableByExt($filePath);
		(new File($filePath))->delete();
		if (($dt->Rows->size() == 0))
		{
			return "err@无导入的数据";
		}
		$bill = FrmBill::constructor__String($this->getFrmID());
		$rpts = new GEEntitys($this->getFrmID());
		$en = new GEEntity($this->getFrmID());
		$noColName = "";
		$nameColName = "";
		$map = $en->getEnMap();
		$attr = $map->GetAttrByKey("BillNo");
		$noColName = $attr->getDesc();
		$codeStruct = $bill->getEnMap()->getCodeStruct();
		$attr = $map->GetAttrByKey("Title");
		$nameColName = $attr->getDesc();
		$attrs = $map->getAttrs();
		$impWay = $this->GetRequestValInt("ImpWay");
		$count = 0;
		$changeCount = 0;
		$successInfo = "";
		if (($impWay == 0))
		{
			$rpts->ClearTable();
			$myen = new GEEntity($this->getFrmID());
			foreach ($dt->Rows as $dr) 			{
				$no = $dr->get($noColName);
				$name = $dr->get($nameColName);
				$myen->setOID(0);
				if (!$DataType->IsNullOrEmpty($codeStruct))
				{
					$no = $StringHelper->padLeft($no, $Integer->parseInt($codeStruct), '0');
				}
				$myen->SetValByKey("BillNo", $no);
				if (($myen->Retrieve("BillNo", $no) == 1))
				{
					$errInfo += (((("err@编号[" . $no) . "][") . $name) . "]重复.");
					continue;
				}
				$errInfo += $this->SetEntityAttrVal($no, $dr, $attrs, $myen, $dt, 0);
				++$count;
				$successInfo += (((((((("&nbsp;&nbsp;<span>" . $noColName) . "�?") . $no) . ",") . $nameColName) . "�?") . $name) . "的导入成�?</span><br/>");
			}
		}
		if ((($impWay == 1) || ($impWay == 2)))
		{
			foreach ($dt->Rows as $dr) 			{
				$no = $dr->get($noColName);
				$name = $dr->get($nameColName);
				if (!$DataType->IsNullOrEmpty($codeStruct))
				{
					$no = $StringHelper->padLeft($no, $Integer->parseInt($codeStruct), '0');
				}
				$myen = ( ($rpts->getNewEntity() instanceof GEEntity) ? $rpts->getNewEntity() : NULL );
				$myen->SetValByKey("BillNo", $no);
				if (($myen->Retrieve("BillNo", $no) == 1))
				{
					$errInfo += $this->SetEntityAttrVal($no, $dr, $attrs, $myen, $dt, 1);
					++$changeCount;
					$successInfo += (((((((("&nbsp;&nbsp;<span>" . $noColName) . "�?") . $no) . ",") . $nameColName) . "�?") . $name) . "的更新成�?</span><br/>");
					continue;
				}
				$errInfo += $this->SetEntityAttrVal($no, $dr, $attrs, $myen, $dt, 0);
				++$count;
				$successInfo += (((((((("&nbsp;&nbsp;<span>" . $noColName) . "�?") . $no) . ",") . $nameColName) . "�?") . $name) . "的导入成�?</span><br/>");
			}
		}
		return (((((((((("errInfo=" . $errInfo) . "@Split") . "count=") . $count) . "@Split") . "successInfo=") . $successInfo) . "@Split") . "changeCount=") . $changeCount);
	}
	protected function SetEntityAttrVal ($no, $dr, $attrs, $en, $dt, $saveType) // [String no, DataRow dr, Attrs attrs, GEEntity en, DataTable dt, int saveType]
	{
		if (($saveType == 0))
		{
			$OID = $this->MyDict_CreateBlankDictID();
			$en->setOID($Long->parseLong($OID));
			$en->RetrieveFromDBSources();
		}
		$errInfo = "";
		foreach ($attrs as $item) 		{
			if ($item->getKey()->equals("BillNo"))
			{
				$en->SetValByKey($item->getKey(), $no);
				continue;
			}
			if ($item->getKey()->equals("Title"))
			{
				$en->SetValByKey($item->getKey(), $dr->get($item->getDesc()));
				continue;
			}
			if (($dt->Columns->contains($item->getDesc()) ==  FALSE ))
			{
				continue;
			}
			if (($item->getMyFieldType() == $FieldType->Enum))
			{
				$val = $dr->get($item->getDesc());
				$se = new SysEnum();
				$i = $se->Retrieve($SysEnumAttr->EnumKey, $item->getUIBindKey(), $SysEnumAttr->Lab, $val);
				if (($i == 0))
				{
					$errInfo += (((((("err@枚举[" . $item->getKey()) . "][") . $item->getDesc()) . "]，�?�[") . $val) . "]不存�?.");
					continue;
				}
				$en->SetValByKey($item->getKey(), $se->getIntKey());
				continue;
			}
			if (($item->getMyFieldType() == $FieldType->FK))
			{
				$val = $dr->get($item->getDesc());
				$attrEn = $item->getHisFKEn();
				$i = $attrEn->Retrieve("Name", $val);
				if (($i == 0))
				{
					$errInfo += (((((("err@外键[" . $item->getKey()) . "][") . $item->getDesc()) . "]，�?�[") . $val) . "]不存�?.");
					continue;
				}
				if (($i != 1))
				{
					$errInfo += (((((("err@外键[" . $item->getKey()) . "][") . $item->getDesc()) . "]，�?�[") . $val) . "]重复..");
					continue;
				}
				$en->SetValByKey($item->getKey(), $attrEn->GetValByKey("No"));
				continue;
			}
			if (($item->getMyDataType() == $DataType->AppBoolean))
			{
				$val = $dr->get($item->getDesc());
				if (($val->equals("�?") || $val->equals("�?")))
				{
					$en->SetValByKey($item->getKey(), 1);
				}
				else
				{
					$en->SetValByKey($item->getKey(), 0);
				}
				continue;
			}
			$myval = $dr->get($item->getDesc());
			$en->SetValByKey($item->getKey(), $myval);
		}
		$en->SetValByKey("BillState", BillState::$Editing->getValue());
		$en->Update();
		return $errInfo;
	}
	protected function DoDefaultMethod () 
	{
		switch ($this->getDoType()) {
			case "DtlFieldUp":
				return "执行成功.";
			default:
				break;
		}
		throw new \RuntimeException(((("@标记[" . $this->getDoType()) . "]，没有找�?. @RowURL:") . $CommonUtils->getRequest()->getRequestURI()));
	}
	public function MethodDocDemoJS_Init () 
	{
		$func = MethodFunc::constructor__String($this->getMyPK());
		return $func->getMethodDoc_JavaScript_Demo();
	}
	public function MethodDocDemoSQL_Init () 
	{
		$func = MethodFunc::constructor__String($this->getMyPK());
		return $func->getMethodDoc_SQL_Demo();
	}
}
?>
